package bairui.international.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lianjing.sh.project.wx.miniapp.model.domain.jpa.TBSuper_WX_App_UserInfo;
import com.lianjing.sh.wx.mp.miniapp.tool.Util_WxAppLogin.Model_UserInfoData;

import bairui.international.model.domain.jpa.TB_BranchCompanyStaff;
import bairui.international.model.domain.jpa.TB_User;
import bairui.international.model.status.State_User;
import bairui.international.model.type.Type_User;
import bairui.international.service.Service_User;
import bairui.international.service.Service_UserAmount;

/**
 * 
 * 【(代理)TB_User】服务实现
 *
 * @generated
 * 
 * @see bairui.international.model.domain.jpa.TB_User 模型
 * @see Service_User 服务接口
 * @see BService_UserImpl 服务实现基础
 *
 */
@Service
public class Service_UserImpl extends BService_UserImpl implements Service_User {

	@Autowired private Service_UserAmount service_UserAmount;
	
	@Override
	public TB_User save(Model_UserInfoData userInfo, TB_User input) {
		TBSuper_WX_App_UserInfo parentData = super.parent_service.newInstance(userInfo);
		TB_User user = input.update(parentData);
			user.setType(Type_User.General);
			user =  super.parent_service.save(
				dao, 
				user
			);
		// 建立账户数据
		service_UserAmount.createInstance(user, 0D);
		//
		return user;
	}
	
	@Override
	public TB_User update(long id, Model_UserInfoData userInfo) throws Exception {
		TB_User bean = findById(id);
		parent_service.updateProperties(bean, userInfo);
		// 建立账户数据
		if(null == bean.getUserAmount()){
			service_UserAmount.createInstance(bean, 0D);
		}
		return bean;
	}

	@Override
	public void upStrategyPartner(long id) {
		TB_User bean = findById(id);
		bean.setType(Type_User.StrategyPartner);
	}

	@Override
	public void unFreeze(long id) throws Exception {
		TB_User bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_User getState(TB_User it) throws Exception {
				return it.getState().t_unfreeze_from_Freezed().to();
			}
		};
	}

	@Override
	public void freeze(long id) throws Exception {
		TB_User bean = findById(id);
		bean.new UpdateProperties() {
			@Override public State_User getState(TB_User it) throws Exception {
				return it.getState().t_freeze_from_Normaled().to();
			}
		};		
	}

	@Override
	public void upPartner(long id) {
		TB_User bean = findById(id);
		bean.setType(Type_User.Partner);		
	}

	@Override
	public void upAgent(long id) {
		TB_User bean = findById(id);
		bean.setType(Type_User.Agent);		
	}
	
	@Override
	public void upSaler(long id) {
		TB_User bean = findById(id);
		bean.setType(Type_User.Saler);		
	}
	
	@Override
	public TB_User setType(String skey, Type_User type) {
		TB_User bean = findOne(it->it.skey.eq(skey));
			bean.setType(type);
		return bean;
	}

	@Override
	public void setDimensionalUrl(long id, String fileName) {
		findById(id)
			.setDimensionalUrl(fileName);
	}

	@Override
	public TB_User edit(Long id, String name, String phone, String company, String accountNumber, String bank) {
		TB_User bean = findById(id);
			if(null != name){
				bean.setRealName(name);
			}
			if(null != phone){
				bean.setPhone(phone);
			}
			if(null != company){
				bean.setCompany(company);
			}
			if(null != accountNumber){
				bean.setAccountNumber(accountNumber);
			}
			if(null != bank){
				bean.setBank(bank);
			}
		return bean;
	}

	@Override
	public void bindSender(TB_User binder, TB_BranchCompanyStaff branchCompanyStaff) {
		load(binder).setSender(branchCompanyStaff);
	}

}
